<?php

class InventoryRentManager implements InventoryManager
{
    private $selectCount;

    public function SelectAllWithoutNames()
    {
        $query = "SELECT * FROM inventory_rent";
        $result = Core::DBConn()->Query($query);
        return array_map("self::CreateInventoryRent", $result->GetAllAssoc());
    }

    public function SelectAll()
    {
        $query = "
        SELECT inventory_rent.*,
        TO_DAYS(inventory_rent.return_date) - TO_DAYS(inventory_rent.rent_date) AS rent_duration,
        client.name AS client_name,
        inventory.name AS inventory_name
         FROM inventory_rent
         JOIN client
         ON inventory_rent.client_id = client.client_id
         JOIN inventory
         ON inventory_rent.inventory_id = inventory.inventory_id";
        $result = Core::DBConn()->Query($query);
        $this->selectCount = $result->Count();
        return array_map("self::CreateInventoryRent", $result->GetAllAssoc());
    }

    public function GetSelectCount()
    {
        return $this->selectCount;
    }

    public static function GetTotalRevenue()
    {
        $query = "SELECT SUM(inventory.cost_per_day * (TO_DAYS(inventory_rent.return_date) - TO_DAYS(inventory_rent.rent_date))) AS total_revenue
            FROM inventory_rent, inventory
            WHERE inventory.inventory_id = inventory_rent.inventory_id";
        $queryResult = Core::DBConn()->Query($query);
        $assoc = $queryResult->GetAssoc();
        return $assoc['total_revenue'];
    }

    public function Insert(InventoryRent $inventoryRent)
    {
        $query = "INSERT INTO inventory_rent
                  SET
                    inventory_id = @inventory_id,
                    client_id = @client_id,
                    rent_date = @rent_date,
                    return_date = @return_date;";
        $db = Core::DBConn();
        $substitutions = array(
            '@inventory_id' => $db->Quote($inventoryRent->GetInventoryId()),
            '@client_id' => $db->Quote($inventoryRent->GetClientId()),
            '@rent_date' => $db->Quote($inventoryRent->GetRentDate()),
            '@return_date' => $db->Quote($inventoryRent->GetReturnDate())
        );
        $db->Query($query, $substitutions);
    }

    public function Update(InventoryRent $inventoryRent)
    {
        $query = "UPDATE inventory_rent
                  SET
                    inventory_id = @inventory_id,
                    client_id = @client_id,
                    rent_date = @rent_date,
                    return_date = @return_date
                  WHERE
                    inventory_rent_id = @id;";
        $db = Core::DBConn();
        $substitutions = array(
            '@id' => $db->Quote($inventoryRent->GetId()),
            '@inventory_id' => $db->Quote($inventoryRent->GetInventoryId()),
            '@client_id' => $db->Quote($inventoryRent->GetClientId()),
            '@rent_date' => $db->Quote($inventoryRent->GetRentDate()),
            '@return_date' => $db->Quote($inventoryRent->GetReturnDate())
        );
        $db->Query($query, $substitutions);
    }

    public function Delete($itemId)
    {
        $query = "DELETE FROM inventory_rent WHERE inventory_rent_id = %s";
        $db = Core::DBConn();
        $db->Query($query, $db->Quote($itemId));
    }

    private static function CreateInventoryRent($dbRecord)
    {
        $inventoryRent = new InventoryRent();
        $inventoryRent->SetId($dbRecord['inventory_rent_id']);
        $inventoryRent->SetInventoryId($dbRecord['inventory_id']);
        $inventoryRent->SetClientId($dbRecord['client_id']);
        $inventoryRent->SetRentDate($dbRecord['rent_date']);
        $inventoryRent->SetReturnDate($dbRecord['return_date']);
        $clientName = Util::GetArrayCorrectString($dbRecord, 'client_name', '');
        $inventoryName = Util::GetArrayCorrectString($dbRecord, 'inventory_name', '');
        $inventoryRent->SetClientName($clientName);
        $inventoryRent->SetInventoryName($inventoryName);
        $inventoryRent->SetRentDuration($dbRecord['rent_duration']);
        return $inventoryRent;
    }


}